package de.bensda.auswertung;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Auswertung {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String fileLocation = "E:\\Studium\\Diplom-Arbeit, die Zweite\\Fragebögen\\data_bensDA2011_2011-10-17.csv";
		File file = new File(fileLocation);
		String destFileLocation = "E:\\Studium\\Diplom-Arbeit, die Zweite\\Fragebögen\\auswertung.csv";
		File destFile = new File(destFileLocation);
		BufferedReader br = null;
		BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
		try {
			br = new BufferedReader(new FileReader(file));
			Map<String, String[]> frageboegen = new TreeMap<String, String[]>();
			String line = br.readLine();
			while((line = br.readLine()) != null){
				String[] seperatedLine = line.split(";");
				frageboegen.put(seperatedLine[0], seperatedLine);
			}
			Map<Integer,Integer> situationMessageMap = getSituationMessageMap();
			Set<String> keys = frageboegen.keySet();
			Double[][] methodMatrix = new Double[5][5];
			Double[][] occurenceMatrix = new Double[5][5];
			for(String key : keys){
				String[] bogen = frageboegen.get(key);
				Map<Integer,Integer[]> importanceMethodMap = new TreeMap<Integer, Integer[]>();
				int messageCounter = 0;
				for(int i=9;i<36;i += 2){
						int nachrichtID = i-8-messageCounter;
						int importance = new Integer(bogen[i]);
						int method = new Integer(bogen[i+1]);
						Integer[] importanceMethod = new Integer[2];
						importanceMethod[0] = importance;
						importanceMethod[1] = method;
						importanceMethodMap.put(nachrichtID, importanceMethod);
						messageCounter++;
				}
				Map<Integer,Integer[]> urgencyMethodMap = new TreeMap<Integer, Integer[]>();
				int situationCounter = 0;
				for(int i=37;i<bogen.length;i += 2){
					int situationID = i-36-situationCounter;
					int urgency = new Integer(bogen[i]);
					int method = new Integer(bogen[i+1]);
					Integer[] urgencyMethod = new Integer[2];
					urgencyMethod[0] = urgency;
					urgencyMethod[1] = method;
					urgencyMethodMap.put(situationID, urgencyMethod);
					int importanceID = importanceMethodMap.get(situationMessageMap.get(situationID))[0]-1;
					int urgencyID = urgency-1;
					if(methodMatrix[importanceID][urgencyID] == null){
						methodMatrix[importanceID][urgencyID] = method * 1.0;
						occurenceMatrix[importanceID][urgencyID] = 1.0;
					} else {
						methodMatrix[importanceID][urgencyID] += method;
						occurenceMatrix[importanceID][urgencyID]++;
					}
					situationCounter++;
					if(urgency == 5 && importanceID == 0){
						System.out.println(situationID + "   :    " + method);
					}
				}
			}
			bw = new BufferedWriter(new FileWriter(destFile));
			bw.write("Wichtigkeit\\Dringlichkeit;1;2;3;4;5");
			bw.newLine();
			for(int j = 0;j<methodMatrix.length;j++){
				for(int m = 0;m<methodMatrix[0].length;m++){
					if(methodMatrix[j][m] == null){
						methodMatrix[j][m] = 0.0;
						occurenceMatrix[j][m] = 1.0;
					}
					methodMatrix[j][m] = methodMatrix[j][m] / occurenceMatrix[j][m];
				}
				double method1 = Math.round(methodMatrix[j][0] * 10) / 10.0;
				double method2 = Math.round(methodMatrix[j][1] * 10) / 10.0;
				double method3 = Math.round(methodMatrix[j][2] * 10) / 10.0;
				double method4 = Math.round(methodMatrix[j][3] * 10) / 10.0;
				double method5 = Math.round(methodMatrix[j][4] * 10) / 10.0;
				String csvLine = (j+1) + ";"+method1+";"+method2+";"+method3+";"+method4+";"+method5;
				bw.write(csvLine.replaceAll("\\.", ","));
				bw.newLine();
			}
			bw.newLine();
			bw.newLine();
			bw.write("Wichtigkeit\\Dringlichkeit;1;2;3;4;5");
			bw.newLine();
			for(int j = 0;j<methodMatrix.length;j++){
				double method1 = Math.round(occurenceMatrix[j][0] * 10) / 10.0;
				double method2 = Math.round(occurenceMatrix[j][1] * 10) / 10.0;
				double method3 = Math.round(occurenceMatrix[j][2] * 10) / 10.0;
				double method4 = Math.round(occurenceMatrix[j][3] * 10) / 10.0;
				double method5 = Math.round(occurenceMatrix[j][4] * 10) / 10.0;
				String csvLine = (j+1) + ";"+method1+";"+method2+";"+method3+";"+method4+";"+method5;
				bw.write(csvLine.replaceAll("\\.", ","));
				bw.newLine();
			}
			bw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(br != null){
				br.close();
			}if(bw != null){
				bw.close();
			}
		}

	}

	private static Map<Integer, Integer> getSituationMessageMap() {
		Map<Integer,Integer> situationMessageMap = new TreeMap<Integer, Integer>();
		situationMessageMap.put(1, 1);
		situationMessageMap.put(2, 6);
		situationMessageMap.put(3, 1);
		situationMessageMap.put(4, 4);
		situationMessageMap.put(5, 2);
		situationMessageMap.put(6, 5);
		situationMessageMap.put(7, 8);
		situationMessageMap.put(8, 9);
		situationMessageMap.put(9, 13);
		situationMessageMap.put(10, 11);
		situationMessageMap.put(11, 3);
		situationMessageMap.put(12, 4);
		situationMessageMap.put(13, 10);
		situationMessageMap.put(14, 6);
		situationMessageMap.put(15, 5);
		situationMessageMap.put(16, 12);
		situationMessageMap.put(17, 1);
		situationMessageMap.put(18, 9);
		situationMessageMap.put(19, 13);
		situationMessageMap.put(20, 6);
		situationMessageMap.put(21, 4);
		situationMessageMap.put(22, 9);
		situationMessageMap.put(23, 2);
		situationMessageMap.put(24, 3);
		situationMessageMap.put(25, 14);
		situationMessageMap.put(26, 12);
		situationMessageMap.put(27, 7);
		situationMessageMap.put(28, 11);
		situationMessageMap.put(29, 10);
		return situationMessageMap;
	}

}
